
// Dom7
var $$ = Dom7;
//Theme
var theme = 'ios';

// Framework7 App main instance
var app = new Framework7({
  root: '#app', // App root element
  id: 'io.framework7.testapp', // App bundle ID
  name: 'LANCE-SHOP', // App name
  theme: theme, // Automatic theme detection
  dialog: {
	  	title: 'My App',
	    buttonOk:'确定',
	    buttonCancel:'取消'
  },
  statusbar: {
	    iosOverlaysWebView: true,
  },
  sheet: {
	    closeByBackdropClick: false,
	    backdrop:true,
   },
  // App root data
  data: function () {
    return {
      user: {
        firstName: 'John',
        lastName: 'Doe',
      },
      // Demo products for Catalog section
      products: [
        
      ]
    };
  },
  // App root methods
  methods: {
    helloWorld: function () {
      app.dialog.alert('Hello World!');
    },
  },
  on: {
    // each object key means same name event handler
    pageInit: function (page) {
    },
    popupOpened: function (popup) {
    },
    routeChanged:function(newRoute, previousRoute, router){
    	var currentUrl = newRoute.url;
    	if(currentUrl=="/order/confirm"||currentUrl=='/message'||currentUrl=='/home/search'){
    		$$(".bottom-bar").hide();
    	}else{
    		$$(".bottom-bar").show();
    	}
    }
  },
  // App routes
  routes: routes,
});

app.request.setup({
	headers: {
        Authentication: 'Bearer '+ window.localStorage.token
     },
	beforeSend:function(){
		app.preloader.show();
	},
	complete:function(){
		setTimeout(function () {
		    app.preloader.hide();
		}, 50);
	}
	
});

var mainView = app.views.create('.view-main',{url: '/',router:true,uniqueHistory:true});
var cartView = app.views.create('.view-cart',{url: '/cart',router:true,uniqueHistory:true});
var personView = app.views.create('.view-person',{url: '/center',router:true,uniqueHistory:true});

function showHomeSlide(){
	
	var slideListRequest = [
	                            {img:"img/f7-icon.png"},
	                            {img:"img/f7-icon.png"}
	                           ];
	

       var homeSlideTemplate = $$('#homeSlideTemplate').html();
       var compiledHomeSlideTemplate = Template7.compile(homeSlideTemplate);
       var innerHTML = compiledHomeSlideTemplate({slideList:slideListRequest});
       $$(".home-slide").append(innerHTML);
       var swiper = app.swiper.create('.home-swiper-container', {
 	        speed: 400,
 	        spaceBetween: 0,
 	        loop:true,
 	         autoplay: {
 	    	    delay: 3000,
 	    	  },
 	    });
       
 	    swiper.on('slideNextTransitionEnd', function () {
 	    	
 	    });
	
}


function listAllProducts(start,pageSize,type){
	 var hasMore = true;
	 var productListResult = [];
  	  var formData= tojsonarray("1");
        app.request({
          url: "js/product-list.json",
          method: 'POST',
          contentType: 'application/json',
          async:false,
          data: formData,
          processData: true,
          success: function (data) {
          	//var r  =JSON.parse(data).r;
        	  var r = JSON.parse(data);
          	if(r.length<pageSize){
         		hasMore = false;
         	}
          	var itemNum = parseInt(r.length/2);
          	var itemModel = r.length%2;
          	for(var i=0;i<itemNum;i++){
          		if(i==0){
          			productListResult.push({item:[r[0],r[1]]});
          		}else{
          			productListResult.push({item:[r[i+1],r[i+2]]});
          		}
          	}
          	if(itemModel!=0){
          		productListResult.push({item:[r[r.length-1]]});
          	}
          },
          error:function(data){
       	   app.preloader.hide();
       	   app.dialog.alert(data.response,function(){
	    	});
         }
       });
        
        var homeProductListTemplate = $$('#productListTemplate').html();
        var compiledHomeProductListTemplate = Template7.compile(homeProductListTemplate);
        var innerHTML = compiledHomeProductListTemplate({productList:productListResult});
       
         if(type=='ap'){//添加
        	 $$(".home-product-list-container").append(innerHTML);
         }else if(type=='rw'){
        	 $$(".home-product-list-container").html(innerHTML);
         }else{
       	  	app.dialog.alert("未知数据显示类型["+type+"]");
         }
         
}


listAllProducts(0,10,"ap");



showHomeSlide();


app.statusbar.setBackgroundColor("#FF852A");